package cn.blb.service.edu.controller;


import cn.blb.common.result.Result;
import cn.blb.service.edu.entity.Chapter;
import cn.blb.service.edu.entity.Video;
import cn.blb.service.edu.entity.dto.ChapterDto;
import cn.blb.service.edu.service.ChapterService;
import cn.blb.service.edu.service.VideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author ccl
 * @since 2022-03-17
 */
@CrossOrigin
@RestController
@RequestMapping("/edu/chapter")
public class ChapterController {
    private final VideoService videoService;
    private final ChapterService chapterService;

    public ChapterController(ChapterService chapterService, VideoService videoService) {
        this.chapterService = chapterService;
        this.videoService = videoService;
    }

    @PostMapping("getChapterById/{id}")
    public Result getChapterById(@PathVariable String id) {
        QueryWrapper<Chapter> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        Chapter chapter = chapterService.getById(id);
        return Result.ok().setData("item", chapter);
    }

    @PostMapping("nestedListChapter/{courseId}")
    public Result nestedListChapter(@PathVariable String courseId) {
        QueryWrapper<Chapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        List<Chapter> list = chapterService.list(wrapper);
        ArrayList<ChapterDto> chapterDtos = new ArrayList<>();
        list.forEach(
                chapter ->
                {
                    QueryWrapper<Video> videoQueryWrapper = new QueryWrapper<>();
                    videoQueryWrapper.eq("chapter_id", chapter.getId());
                    chapterDtos.add(new ChapterDto(chapter).setVideoVos(videoService.list(videoQueryWrapper)));
                }
        );
        return Result.ok().setData("items", chapterDtos);
    }

    @PostMapping("addChapter")
    public Result saveChapter(@RequestBody Chapter chapter) {
        System.out.println("chapter = " + chapter);
        return chapterService.save(chapter) ? Result.ok() : Result.error().setMessage("添加失败");
    }

    @PostMapping("updateChapterById")
    public Result updateChapterById(@RequestBody Chapter chapter) {
        return chapterService.updateById(chapter) ? Result.ok() : Result.error();
    }

    @DeleteMapping("{id}")
    @Transactional(rollbackFor = RuntimeException.class)
    public Result removeChapterById(@PathVariable String id) {
        boolean remove = true;
        QueryWrapper<Video> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", id);
        List<Video> list = videoService.list(wrapper);
        //获取所有video，如果没有删除chapter，如果有先删除video，在上一步成功下删除chapter
        if (list.size() > 0) {
            remove = videoService.removeBatchByIds(list);
        }

        if (remove) {
            remove = chapterService.removeById(id);
        }
        return remove ? Result.ok() : Result.error().setMessage("删除失败");
    }

}


